package com.sunhy.cpdemo1.common;

import com.sunhy.cpdemo1.entity.Article;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.sql.DataSource;
import java.lang.reflect.Method;
import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

@Aspect
@Component
public class CpAspect {

    @Autowired
    DataSource dataSource;

    @Pointcut("@annotation(com.sunhy.cpdemo1.common.CpAnnotation)")
    public void pointcut() { }

    @Around("pointcut()")
    public Object log(final ProceedingJoinPoint joinPoint) throws Throwable {
        // Method
        MethodSignature signature = (MethodSignature) joinPoint.getSignature();
        Method method = signature.getMethod();
        //获取方法上的Aop注解
        CpAnnotation annotation = method.getAnnotation(CpAnnotation.class);
        String sql = annotation.sql();
        String type = annotation.type();
        // 执行数据库查询逻辑
        Connection connection = dataSource.getConnection();
        if ("SELECT".equals(type)) {
            PreparedStatement preparedStatement = connection.prepareStatement(sql);
            ResultSet rs = preparedStatement.executeQuery();
            List<Article> list = new ArrayList<>();
            while (rs.next()) {
                Article article = new Article();
                article.setId(rs.getLong("id"));
                article.setAuthor(rs.getString("author"));
                article.setTitle(rs.getString("title"));
                article.setContent(rs.getString("content"));
                article.setDate(rs.getDate("date"));
                list.add(article);
            }
            rs.close();
            System.out.println(list);
        } else if ("INSERT".equals(type)) {
            PreparedStatement preparedStatement = connection.prepareStatement(sql);
            int i = preparedStatement.executeUpdate();
            System.out.println("影响了" + i + "行");
        } else if ("INSERT_2".equals(type)) {
            Object[] args = joinPoint.getArgs();
            String insertSql = "INSERT INTO `article`(`author`, `title`, `content`, `date`) VALUES (?, ?, ?, ?) ";
            Article article = null;
            if (args[0] instanceof Article) {
                article = (Article) args[0];
            }
            PreparedStatement preparedStatement = connection.prepareStatement(insertSql);
            preparedStatement.setString(1, article.getAuthor());
            preparedStatement.setString(2, article.getTitle());
            preparedStatement.setString(3, article.getContent());
            preparedStatement.setDate(4, (Date) article.getDate());
            int i = preparedStatement.executeUpdate();
            System.out.println("影响了" + i + "行");
        }



        return null;
    }


}
